package com.woniuxy.productweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.productdao.model.TeamReservation;
import com.woniuxy.productmodel.dto.TeamReservDto;
import com.woniuxy.productmodel.form.VenueListTeamReservForm;
import com.woniuxy.productservice.service.TeamReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 团课预约表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Api(tags = {"团课预约资源接口"})
@RestController
@RequestMapping("/teamReservation")
public class TeamReservationController {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", 1);
        String token = MyJwtUtil.createUserToken(map, 200);
//        map.put("userId",1);
//        String token = MyJwtUtil.createUserToken(map, 80);
        System.out.println(token);
    }
    @Autowired
    private TeamReservationService teamReservationService;

    /**
     * 用户预约团课
     * @param teamProductId 团课产品id
     * @param token 用户登录token
     * @return
     */
    @ApiOperation("用户预约团课")
    @GetMapping("/userAdd")
    public Result<TeamReservDto> addTeamReservation(Integer teamProductId, @RequestHeader String token) throws InterruptedException {
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        TeamReservDto dto = teamReservationService.addTeamReservation(teamProductId,userId);
        return Result.success("预约团课成功",dto);
    }

    /**
     * 用户查看自己的团课预约成功
     * @param token 用户登录token
     * @return
     */
    @ApiOperation("用户查看自己团课预约")
    @GetMapping("/userList")
    public Result<List<TeamReservDto>> userListTeamReservation(@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        List<TeamReservDto> dtos = teamReservationService.userListTeamReservation(userId);
        return Result.success("用户查看自己团课预约成功",dtos);
    }

    /**
     * 商家端查看自己团课预约
     * @param token 商家登录token
     */
    @ApiOperation("商家端查看团课预约")
    @PostMapping("/venueList.venue")
    public Result<PageInfo<TeamReservDto>> venueListTeamReservation(@RequestBody VenueListTeamReservForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        PageInfo<TeamReservDto> pageInfo = teamReservationService.venueListTeamReservation(form,venueId);
        return Result.success("商家端查看自己团课预约成功",pageInfo);
    }

    /**
     * 根据id查看团课预约
     * @param reservationId
     * @return
     */
    @ApiOperation("根据id查看团课预约")
    @GetMapping("/findById")
    public Result<TeamReservDto> findTeamReservationById(Integer reservationId){
        TeamReservation teamReservation = teamReservationService.getById(reservationId);
        TeamReservDto dto = BeanUtil.toBean(teamReservation, TeamReservDto.class);
        return Result.success("根据id查看团课预约",dto);
    }

    /**
     * 用户取消团课预约
     * @param reservationId 预约Id
     * @param token 用户登录token
     * @return
     */
    @ApiOperation("用户取消团课预约")
    @GetMapping("/userCancel")
    public Result userCancelTeamReservation(Integer reservationId,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        teamReservationService.userCancelTeamReservation(reservationId,userId);
        return Result.success("用户取消团课预约成功");
    }

    /**
     * 商家端给用户开课
     * @param teamProductId 团课产品id
     * @param token 商家登录token
     * 商家端开课后，所有该团课产品的已预约的预约变成已开课，用户端可以根据预约详情支付已开课的产品成为学员，如果长时间不支付，则取消其预约
     * 商家端可重复开课
     * @return
     */
    @ApiOperation("商家端给团课产品开课")
    @GetMapping("/openClass.venue")
    public Result venueOpenClass(Integer teamProductId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamReservationService.venueOpenClass(teamProductId,venueId);
        return Result.success("商家端给预约该团课产品的用户开课成功");
    }

    /**
     * 订单未支付取消时用的
     * @param state 状态值
     * @param reservationId 预约id
     * @return
     */
    @ApiOperation("修改团课邀请状态")
    @GetMapping("/updateState")
    public Result updateTeamReservState(Integer state,Integer reservationId){
        TeamReservation teamReservation = teamReservationService.getById(reservationId);
        if (null != teamReservation) teamReservation.setReservationState(state);
        teamReservationService.updateById(teamReservation);
        return Result.success("修改团课邀请状态成功");
    }

    @ApiOperation("通过团课产品id查找预约方法")
    @GetMapping("/byProductId")
    public Result<List<TeamReservDto>> byProductId(Integer teamProductId){
        List<TeamReservDto> dtos = teamReservationService.byProductId(teamProductId);
        return Result.success("通过团课产品id查找预约方法成功",dtos);
    }

}

